package com.smart.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smart.model.domain.OperatePermissionDO;
import com.smart.model.request.OperatePermissionRequest;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author zhanghong
 */
@Repository
public interface OperatePermissionMapper extends BaseMapper<OperatePermissionDO> {
    OperatePermissionDO selectDOById(Long id);
    List<OperatePermissionDO> selectDOList(OperatePermissionDO operatePermission);
    int insertDO(OperatePermissionDO operatePermission);
    int batchInsertDO(List<OperatePermissionDO> operatePermissionDOList);
    int updateDO(OperatePermissionDO operatePermission);
    int deleteDOById(Long id);
    int deleteDOByIds(String[] ids);

    @Select({"<script>",
            "   select count(1) from operate_permission",
            "   <where>",
            "       deleted=0",
            "       <if test=\"1==0\">and id=#{param.id}</if>",
            "   </where>",
            "</script>"})
    long pageCount(@Param("param") OperatePermissionRequest param);
    @Select({"<script>",
            "   select * from operate_permission",
            "   <where>",
            "       deleted=0",
            "       <if test=\"1==0\">and id=#{param.id}</if>",
            "   </where>",
            "   order by create_time desc limit #{limit} offset #{offset}",
            "</script>"})
    List<OperatePermissionDO> pageQuery(@Param("param") OperatePermissionRequest param, @Param("limit") int limit, @Param("offset") long offset);

    @Select({"<script>",
            "   select p.* from operate_permission p",
            "   inner join operate_role_permission rp on rp.permission_id=p.id",
            "   <where>",
            "       p.deleted=0 and rp.role_id=#{roleId}",
            "   </where>",
            "</script>"})
    List<OperatePermissionDO> findByRoleId(Long roleId);

    @Select("select * from operate_permission where id = #{id} limit 1")
    OperatePermissionDO findById(Long id);

    @Select("select * from operate_permission where parent_id = #{parentId}")
    List<OperatePermissionDO> findByParentId(long parentId);
}
